package com.mall.user.services;

import com.mall.user.IUserVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Collection;
import java.util.List;

@Service
@Component
public class UserVerifyServiceImpl implements IUserVerifyService {

    @Autowired()
    UserVerifyMapper userVerifyMapper;

    @Autowired
    MemberMapper memberMapper;

    @Override
    public UserVerifyResponse verify(UserVerifyRequest request) {
        UserVerifyResponse userVerifyResponse = new UserVerifyResponse();
        //判空，用户名和uuid是否为空
        request.requestCheck();

        //根据uuid去查询userVerify这个表
        Example example = new Example(UserVerify.class);
        //根据表的字段名来查询，而不是传进来的是uid
        example.createCriteria().andEqualTo("uuid",request.getUuid());
        List<UserVerify> userVerifyList = userVerifyMapper.selectByExample(example);
        //CollectionUtils.isEmpty(null): true 判断集合是否为空
        //下面的这句代码：如果集合为空
        if (CollectionUtils.isEmpty(userVerifyList)){
           userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_INVALID.getCode());
           userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_INVALID.getCode());
           return userVerifyResponse;
        }

        //把两个username进行对比
        UserVerify userVerify = userVerifyList.get(0);
        String userName = request.getUserName();
        if (!userName.equals(userVerify.getUsername())){
            userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_INVALID.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_INVALID.getMessage());
            return userVerifyResponse;
        }
        //比对成功 改userverify的激活字段
        userVerify.setIsVerify("Y");
        int effectedRows1 = userVerifyMapper.updateByExample(userVerify, example);
        if (effectedRows1 < 1){
            userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getMessage());
            return userVerifyResponse;
        }

        //通过username去查询member改member表的激活字段
        Example exampleMember = new Example(Member.class);
        exampleMember.createCriteria().andEqualTo("username",request.getUserName());
        List<Member> members = memberMapper.selectByExample(exampleMember);
        if (CollectionUtils.isEmpty(members)){
            userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getMessage());
            return userVerifyResponse;
        }
        Member member = members.get(0);
        member.setIsVerified("Y");
        int effectedRows2 = memberMapper.updateByExample(member, exampleMember);
        if (effectedRows2 < 1){
            userVerifyResponse.setCode(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getCode());
            userVerifyResponse.setMsg(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getMessage());
            return userVerifyResponse;
        }
        userVerifyResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        userVerifyResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return userVerifyResponse;
    }
}
